//
//  ContactEmail.swift
//  PeoplePickerNavigationController
//
//  Created by 陶建 on 16/5/26.
//  Copyright © 2016年 tao.520it. All rights reserved.
//

import UIKit
import AddressBookUI

class ContactEmail: NSObject {
    // 目前获取不到
    var localizedEmailLabel = ""
    var email = ""
    
    init(emailValues: ABMutableMultiValue, index: Int) {
        // 获得标签名
        let emailLabel = ABMultiValueCopyLabelAtIndex(emailValues, index)?.takeRetainedValue() as? String ?? ""
        // 转为本地标签名（能看得懂的标签名，比如work、home）
        localizedEmailLabel = ABAddressBookCopyLocalizedLabel(emailLabel).takeRetainedValue() as String
        email = ABMultiValueCopyValueAtIndex(emailValues, index).takeRetainedValue() as? String ?? ""
    }
    
    override init() {
        super.init()
    }
    
    class func contactEmails(emailValues: ABMutableMultiValue) -> [ContactEmail] {
        var contactEmails: [ContactEmail] = []
        for i in 0 ..< ABMultiValueGetCount(emailValues){
            let contactEmail = ContactEmail(emailValues: emailValues, index: i)
            contactEmails.append(contactEmail)
        }
        
        return contactEmails
    }
    
    deinit {
        print("ContactEmail: deinit")
    }
}

